rm(list=ls())

#---------------------------------------------------------------#
# PROJECT: HUMAN RIGHTS ABUSES                                  #
# SCRIPT: REPLICATION MANUSCRIPT - FIG 1                        #
# AUTHOR: FLORES MACIAS AND ZARKIN                              #
# DATE: JANUARY 2022                                            #
#---------------------------------------------------------------#

#### ------ STEP 1: LOAD PACKAGES #### 

library(tidyr)
library(tibble)
library(stargazer)
library(dplyr)
library(haven)
library(readstata13)
library(ggplot2)
library(zoo)
library(ggridges)
library(ggpubr)
library(ggplotify)
library(cowplot)

# ---------------STEP 2: LOAD DATA ####

trends <- read.dta13("0_DATA/Trends.dta")
instit <- read.dta13("0_DATA/SeriousByAgency.dta")

# ---------------STEP 3: FIGURE 1 RATE OF SERIOUS HR ABUSE COMPLAINTS )PER 100,000 POP) OVER TIME AND TYPE OF SERIOUS COMPLAINTS BY AGENCY ####

subset<- ggplot(data=trends, aes(x=year, y=subset, group=who)) +
  geom_line(aes(linetype=who)) + 
  xlab("Year") + ylab("Serious HR complaints per 100,000 people") + 
  theme(axis.title.x = element_text(size=14),
        legend.position = "bottom",
        legend.text = element_text(size=14),
        axis.text.x = element_text(size=12),
        axis.text.y = element_text(size=12),
        axis.title.y = element_text(size=14)) + 
  scale_linetype_manual(values=c("solid", "twodash", "dotted")) +
  scale_y_continuous(limits=c(0, 2.5)) + 
  scale_x_continuous(breaks = c(2000, 2001, 2002,
                                2003, 2004, 2005,
                                2006, 2007, 2008,
                                2009, 2010, 2011, 
                                2012, 2013, 2014,
                                2015, 2016)) + 
  labs(linetype = "")

plot(subset)

# ---------------STEP 4: TYPE BY AGENCY ####

instit$type<-factor(instit$type,levels=c("Inhumane treatment/Torture", 
                                         "Arbitrary detention/Illegal retention",
                                         "Intimidation",
                                         "False allegation of a crime",
                                         "Other"))


instit_fig<- ggplot(data = instit) + 
  geom_bar(aes(fill=type, y=pc, x=institution), position="stack", stat="identity", width=.9,color="black") + 
  ylab("Percentage") + 
  xlab("Agency") + 
  theme(legend.position = "right",
        panel.background = element_rect(fill = "#f7f7f7",colour = "grey50"),
        axis.text.x = element_text(size = 12, angle=0),
        axis.text.y = element_text(size = 12),
        axis.title.x = element_text(size =14),
        axis.title.y = element_text(size =14),
        legend.text = element_text(size=12)) + 
  labs(fill = "") +
  scale_fill_manual(values=c("#f7f7f7","#cccccc","#969696","#636363","#252525"))

plot(instit_fig)

plots2 <-  ggarrange(
  subset, instit_fig)
plot(plots2)

    # CHANGE FILE NAME BELOW

ggexport(plots2, 
         filename="2_FIGURES/FIG1.png",
         width = 1200,
         height =500)
